const express = require('express');
const router = express.Router();
const { getToken } = require('../token/token');
const executeQuery = require('../MySQL/connect');

router.delete('/:id', async (req, res) => {
    const tokenResult = getToken(req.headers.authorization);
    
    if (!tokenResult.boolean) {
        return res.status(405).send(tokenResult.msg);
    }

    if (!tokenResult.isAdmin) {
        return res.status(401).send('你不是管理员，无法删除');
    }

    const studentId = req.params.id;

    try {
        // 在这里执行删除学生的操作，假设你的学生信息存储在名为 "students" 的表中
        const deleteStudentQuery = `DELETE FROM students WHERE id = ?`;
        await executeQuery(deleteStudentQuery, [studentId]);

        res.status(200).send('学生删除成功');
    } catch (error) {
        console.error(error);
        res.status(500).send('删除学生时出现错误');
    }
});

module.exports = router;
